package com.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.dao.BuyerDao;
import com.shop.entity.Buyer;
import com.shop.entity.Result;
import com.shop.enums.EffectiveEnum;
import com.shop.service.BuyerManagerService;
import com.shop.util.ResultUtil;
import com.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * description: 该文件说明
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/9/29 17:04
 **/
@Service
public class BuyerManagerServiceImp implements BuyerManagerService {

    @Resource
    private BuyerDao buyerDao;
    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    /**
     * description:<分页查找会员列表>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/9 14:35
     * @param  
     * @return 
     **/
    @Override
    public PageInfo<Buyer> getBuyerList(Integer pages,Integer rows,Buyer buyer) {
        PageHelper.startPage(pages,rows);
        List<Buyer> buyerList = buyerDao.getBuyerList(buyer);
        return new PageInfo<Buyer>(buyerList);
    }

    /**
     * description:<添加会员>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/9 14:35
     * @param  
     * @return 
     **/
    @Override
    public Result addBuyer(Buyer buyer) {
        //校验数据
        Result result = checkData(buyer);
        if(result.getCode() == ResultUtil.ERROR){
            return  result;
        }
        //设置主键
        buyer.setBuyerId(UUIDUtil.uuid());
        try {
            buyerDao.addBuyer(buyer);
        } catch (Exception e) {
            return ResultUtil.error("添加会员系统出错，请联系管理员");
        }
        return ResultUtil.success("添加会员成功");
    }

    /**
     * description:<修改会员>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/9 17:10
     * @param  
     * @return 
     **/
    @Override
    public Result modifyBuyer(Buyer buyer) {
        //校验数据
        Result result = checkData(buyer);
        if(result.getCode() == ResultUtil.ERROR){
            return  result;
        }
        try {
            buyerDao.modifyBuyer(buyer);
        } catch (Exception e) {
            return ResultUtil.error("修改会员系统出错，请联系管理员");
        }
        return ResultUtil.success("修改会员成功");
    }

    /**
     * description:<删除或者恢复会员>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/10 10:02
     * @param  
     * @return 
     **/
    @Override
    public Result deleteOrRestoreBuyer(String buyerID) {
        //1.检查id是否为空
        if(StringUtils.isEmpty(buyerID)){
            return ResultUtil.error("会员id为空");
        }
        //2.查找id对应的会员是否存在

        Buyer buyer = null;
        try {
            buyer = buyerDao.findBuyerByBuyerID(buyerID);
        } catch (Exception e) {
            return  ResultUtil.error("系统出错，请联系管理员");
        }
        if(null == buyer){
           //如果找不到对应的会员就返回相应的提示消息
            return  ResultUtil.error("所选择的会员不存在，请稍后再试");
        }
        //3.判断会员是有效还是无效，如果是删除状态就恢复，如果是存在状态就删除
        if(EffectiveEnum.BUYER_EFFECTIVE.equal(buyer.getEffective())){
            //如果是有效就修改为无效
            buyer.setEffective(EffectiveEnum.BUYER_DELETE.getCode());
        }else {
            buyer.setEffective(EffectiveEnum.BUYER_EFFECTIVE.getCode());
        }

        try {
            buyerDao.deleteOrRestore(buyer);
        } catch (Exception e) {
            return ResultUtil.error("系统出错，请联系管理员");
        }

        return ResultUtil.success("修改会员状态成功");
    }

    /**
     * description:<通过会员id查找会员>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/10 10:21
     * @param  
     * @return 
     **/
    @Override
    public Buyer findBuyerByBuyerID(String buyerID) {
        return null;
    }

    private Result checkData(Buyer buyer){
        if(StringUtils.isEmpty(buyer.getBuyerAddress())){
              return ResultUtil.error("收货地址不能为空");
        }
        if(StringUtils.isEmpty(buyer.getBuyerName())){
            return ResultUtil.error("会员名称不能为空");
        }
        if(StringUtils.isEmpty(buyer.getBuyerNickName())){
            return ResultUtil.error("会员昵称不能为空");
        }
        if(StringUtils.isEmpty(buyer.getBuyerPassword())){
            return ResultUtil.error("会员密码不能为空");
        }
        String buyerPhoneNumber = buyer.getBuyerPhoneNumber();
        if(StringUtils.isEmpty(buyerPhoneNumber)){
            return ResultUtil.error("会员电话号码不能为空");
        }

        //正则表达校验手机号码
        if( !Pattern.matches(REGEX_MOBILE, buyerPhoneNumber)){
            return ResultUtil.error("会员电话号码格式错误");
        }

        return ResultUtil.success();
    }
}
